package
{
	import flash.display.GradientType;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.DropShadowFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	
	[SWF(width="550",height="400",backgroundColor="0x000000")]
	public class BurningCandles extends Sprite
	{
		private var cir:Sprite = new Sprite();
		private var mmm:Matrix = new Matrix();
		private var vs:Number = 0.05;
		
		private var candle:Sprite = new Sprite();
		private var m:Matrix = new Matrix();
		
		//======= 画灯线
		private var rush:Sprite =new Sprite();
		private var va:Number=0.02;
		
		public function BurningCandles()
		{
			addChild(cir);
			cir.x = 275;
			cir.y = 80;
			
			mmm.createGradientBox(400, 400, 0, -200, -200);
			cir.graphics.beginGradientFill(GradientType.RADIAL, [0xffff99, 0], [1, 0], [0, 25], mmm);
			cir.graphics.drawCircle(0, 0, 300);
			cir.addEventListener(Event.ENTER_FRAME, ff);
			
			addChild(candle);
			candle.x = 275;
			candle.y = 270;
			
			m.createGradientBox(80, 300, Math.PI / 2, 0, -200);
			candle.graphics.beginGradientFill(GradientType.LINEAR, [0xffcccc, 0xfebcbc, 0xfe9a9a, 0xff0000], [1, 1, 1, 1], [0, 18, 36, 255], m);
			candle.graphics.drawRoundRect(-40, -200, 80, 400, 15, 15);
			candle.graphics.beginFill(0xffffff, 0.1);
			candle.graphics.drawRect(-10, -150, 30, 300);
			candle.scaleX = candle.scaleY = 0.6;
			candle.filters = [new DropShadowFilter(20, 275, 0x000000, 1, 20, 20, 1.3, 2, true)];
			candle.transform.colorTransform = new ColorTransform(1, 1, 1, 1, 255, 0, 0, 0);
			
			addEventListener(Event.ENTER_FRAME ,onfiremove);
			
			addChild(rush);
			rush.graphics.lineStyle(2,0xffffff);
			rush.graphics.moveTo(275,130);
			rush.graphics.curveTo(270,175,275,120);
			
			//======= 蜡烛明暗变化
			candle.addEventListener(Event.ENTER_FRAME ,f);
		}
		
		private function ff(e):void
		{
			e.target.scaleX = e.target.scaleY += vs;
			e.target.scaleX < 0.1 ? [e.target.scaleX = 0.1, vs = -vs] : 0;
			e.target.scaleX > 0.5 ? [e.target.scaleX = 0.5, vs = -vs] : 0;
		}
		
		private function firemc():Sprite
		{
			var fireside:Number = 50;
			var fire:Sprite = new Sprite();
			addChild(fire);
			var mm:Matrix = new Matrix();
			mm.createGradientBox(fireside, fireside, 0, -fireside/2, -fireside/2);
			// 参数：放射性，三个颜色值，三个颜色值的透明度，三个颜色值的偏移值，矩阵
			fire.graphics.beginGradientFill(GradientType.RADIAL, [0xfffffcc,0xffcc00,0xff0000],[0.5,0.3,0],[0,100,255],mm);
			fire.graphics.drawCircle(0, 0, fireside/2);
			
			return fire;
		}
		
		private function firemove():Sprite
		{
			var fire:Sprite=firemc();
			fire.blendMode="add";// 设置混合模式， add表示所有火球的颜色都和背景颜色混合
			fire.addEventListener(Event.ENTER_FRAME ,onfire);
			
			function onfire(e):void
			{
				fire.y-=6;
				fire.x+=Math.random()*4-2;
				fire.alpha-=0.05;
				fire.scaleX=fire.scaleY-=0.001;
				if (alpha<=0.005) 
				{
					fire.removeEventListener(Event.ENTER_FRAME ,onfire);
					removeChild(fire);
					fire=null;
				}
			}
			return fire;
		}
		
		private function onfiremove(e):void
		{
			var fire:Sprite=firemove();
			fire.x=275;
			fire.y=130;
		}
		private function f(e):void 
		{
			e.target.alpha-=va;
			if (e.target.alpha<0.9) {
				e.target.alpha=0.9;
				va*=-1;}
			if (e.target.alpha>1) {
				e.target.alpha=1;
				va*=-1;
			}
		}
	}
}